﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;

using iMainAddin;

namespace iAdministerAddin
{
		class iAdministerMenu : iAdminister
		{
            #region MEMBER VARIABLES
            
            private List<CommandBarEvents> menuItemHandlerList = new List<CommandBarEvents>();
            private Dictionary<string, iCommand> cmdList = new Dictionary<string, iCommand>();
            #endregion

            #region MENU CREATION
            public CommandBarPopup CreatePopupMenu(string commandBarName, string menuName)
            {
                CommandBarControls con = (CommandBarControls)GetCommandBar("MenuBar").Controls;
                System.Collections.IEnumerator conEnum = con.GetEnumerator();
                CommandBarPopup menu = GetCommandBar(commandBarName).Controls.Add(MsoControlType.msoControlPopup, Missing.Value, Missing.Value, 1, true) as CommandBarPopup;
                menu.Caption = menuName;
                menu.TooltipText = "";
                return menu;
            }
            public CommandBarPopup CreatePopupMenu(CommandBarPopup popupMenu, string subPopupMenuName, int position)
            {
                CommandBarPopup menu = (CommandBarPopup)popupMenu.Controls.Add(MsoControlType.msoControlPopup, 1, "", position, true);
                menu.Caption = subPopupMenuName;
                menu.TooltipText = "";
                return menu;
            }
            #endregion

            #region MENU ITEMS CREATION
            public void AddCommandMenu(CommandBarPopup popupMenu, iCommand cmd, int position)
            {
                CommandBarControl menuItem = CreateMenuItem(popupMenu, cmd, position);
                AddClickEventHandler(menuItem);
                AddCommandToList(cmd);
            }
			public void AddCommandMenu(string popupMenu, iCommand cmd, int position)
			{
				CommandBarControl menuItem = CreateMenuItem(popupMenu, cmd, position);
				AddClickEventHandler(menuItem);
				AddCommandToList(cmd);
			}

			private CommandBarControl CreateMenuItem(CommandBarPopup menu, iCommand cmd, int position)
            {
				if (menu == null)
					return null;

				CommandBarControl menuItem = menu.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value
						, position, true);

				if (menuItem != null)
				{
					menuItem.Tag = cmd.Id.ToString();
					menuItem.Caption = cmd.Caption;
					menuItem.TooltipText = cmd.TooltipText;
					return menuItem;
				}
				return null;
            }

			private CommandBarControl CreateMenuItem(string CommandBarName, iCommand cmd, int position)
			{
				CommandBar commandBar = GetCommandBar(CommandBarName);
				if (commandBar == null)
					return null;

				CommandBarControl menuItem = commandBar.Controls.Add(MsoControlType.msoControlButton
						, Missing.Value, Missing.Value, position, true);// as CommandBarPopup;

				if (menuItem != null)
				{
					menuItem.Tag = cmd.Id.ToString();
					menuItem.Caption = cmd.Caption;
					menuItem.TooltipText = cmd.TooltipText;
					return menuItem;
				}
				return null;
			}

            private void AddClickEventHandler(CommandBarControl menuItem)
            {
                CommandBarEvents menuItemHandler = (EnvDTE.CommandBarEvents)m_Application.DTE.Events.get_CommandBarEvents(menuItem);
                menuItemHandler.Click += new _dispCommandBarControlEvents_ClickEventHandler(MenuItem_Click);
                menuItemHandlerList.Add(menuItemHandler);
            }

            private void AddCommandToList(iCommand cmd)
            {
                if (!cmdList.ContainsKey(cmd.Id.ToString()))
                {
                    cmdList.Add(cmd.Id.ToString(), cmd);
                }
            }
            #endregion

            #region MENU ITEMS HANDLER
            private void MenuItem_Click(object commandBarControl, ref bool handled, ref bool cancelDefault)
            {
                try
                {
                    // We perform the command only if we found the command corresponding to the menu item clicked
                    CommandBarControl menuItem = (CommandBarControl)commandBarControl;
                    if (cmdList.ContainsKey(menuItem.Tag))
                    {
                        cmdList[menuItem.Tag].Perform();
                    }
                }
                catch// (ExplorerException e)
                {
                    //MessageBox.Show(e.Message, "Explorer add in");
                }
            }
            #endregion
		}
}
